// SyntaxAnalyzer.cpp: implementation of the SyntaxAnalyzer class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "Expression.h"
#include "SyntaxAnalyzer.h"
#include "ComplierNote.h"
#include "Statement.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

SyntaxAnalyzer::SyntaxAnalyzer(const CString& strExpression, PropertyStackInterface* pStack)
{
	ASSERT(pStack);
	m_pStack = pStack;
	m_AAnalyzer.SetStatement(strExpression);
	m_AAnalyzer.SetErrorReport(&m_ErrorReporter);
}

SyntaxAnalyzer::~SyntaxAnalyzer()
{
	ClearStatement();
}

BOOL SyntaxAnalyzer::Interpret()
{
	ClearStatement();
	m_AAnalyzer.MoveFirstSymbol();
	ComplierNote* pNote = new Statement;
	pNote->AAnalyzer(&m_AAnalyzer);
	pNote->PStack(this->m_pStack);
	pNote->EReporter(&m_ErrorReporter);
	m_theStatements.Add(pNote);
	if(!pNote->Interpret())
	{
		m_ErrorReporter.AddErrorInfo(pNote->ErrorInfo());
		return FALSE;
	}
	return TRUE;
}

void SyntaxAnalyzer::ClearStatement()
{
	for(int i = 0; i < m_theStatements.GetSize(); i++)
		delete m_theStatements.GetAt(i);

	m_theStatements.RemoveAll();
}

Variable SyntaxAnalyzer::Execute()
{
	ComplierNote* pNote = NULL;
	Variable variable;

	for(int i = 0; i< m_theStatements.GetSize(); i++)
	{
		pNote = m_theStatements.GetAt(i);
		variable = pNote->Execute();
	}
	return variable;
}

void SyntaxAnalyzer::ReportError()
{
	m_ErrorReporter.Report();
}